package com.iflytek.web.servlet;

import com.alibaba.fastjson.JSONObject;
import com.iflytek.web.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

//最重要的作用就是把url和本类绑定
@WebServlet("/login")
public class LoginServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//规定响应的编码和数据格式(每一个接口都得加，使用filter统一过滤)
//		response.setCharacterEncoding("UTF-8");
//		response.setContentType("application/json");


//1、获取前端传递的参数
		String account = request.getParameter("account");
		String password = request.getParameter("pwd");

		boolean flag = new UserService().loginSuccess(account, password);
		Map<String, Object> map = new HashMap<>();
		if (flag) {
			// 登录成功
			map.put("code", "1000");
			map.put("msg", "登录成功");
		} else {
			// 登录失败
			map.put("code", "1001");
			map.put("msg", "账号或密码不正确，请重新输入");
		}
		String jsonString = JSONObject.toJSONString(map);
		response.getWriter().append(jsonString);
	}
}
